package com.ruoyi.user.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.user.mapper.AnchorApplyMapper;
import com.ruoyi.user.model.constant.UserConstant;
import com.ruoyi.user.model.pojo.entity.AnchorApply;
import com.ruoyi.user.model.pojo.entity.User;
import com.ruoyi.user.service.IAnchorApplyService;
import com.ruoyi.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 * 主持申请 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2024-11-22
 */
@Service
public class AnchorApplyServiceImpl extends ServiceImpl<AnchorApplyMapper, AnchorApply> implements IAnchorApplyService {

    @Autowired
    private IUserService userService;

    @Override
    public void apply(Long userId) {
        Assert.isTrue(this.isUserAnchor(userId) == 0, "您已申请通过请勿重复申请");
        Long count = lambdaQuery().eq(AnchorApply::getUserId, userId).eq(AnchorApply::getStatus, UserConstant.AppStatus.no).count();
        Assert.isTrue(count == 0, "有申请正在审核中请勿重复申请");

        User userInfo = userService.findUserInfo(userId);

        AnchorApply anchorApply = new AnchorApply();
        anchorApply.setUserId(userId);
        anchorApply.setStatus(UserConstant.AppStatus.no);
        anchorApply.setUserName(userInfo.getNickName());
        anchorApply.setUserPhoto(userInfo.getPhoto());
        save(anchorApply);
    }

    @Override
    public Long isUserAnchor(Long userId) {
        return lambdaQuery().eq(AnchorApply::getUserId, userId)
                .eq(AnchorApply::getStatus, UserConstant.AppStatus.yes)
                .count();
    }
}
